﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;

namespace Com.GUI.LibraryManager.Controls.SubControl
{
    /// <summary>
    /// Interaction logic for ucAirConditionGlassRadiationThroughGlass.xaml
    /// </summary>
    public partial class ucAirConditionGlassRadiationThroughGlass : UserControl
    {
        public ucAirConditionGlassRadiationThroughGlass()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            loadData();
        }

        public void loadData()
        {
            cbxDescription.ItemsSource = LibraryManagerInCom.SolarRadiationList;
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;

            cbxDirection.ItemsSource = LibraryManagerInCom.GlassDirectionList;
            cbxDirection.DisplayMemberPath = "glass_direction";
            cbxDirection.SelectedIndex = 0;
                
        }

        public void reload()
        {
            cbxDescription.ItemsSource = null;
            cbxDescription.Items.Clear();
            foreach (var item in LibraryManagerInCom.SolarRadiationList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;

        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_solar_radiation_through_glass so = (tb_solar_radiation_through_glass)cbxDescription.SelectedItem;
            if (so != null)
            {
                cbxMonth.SelectedIndex = Convert.ToInt16(so.month) -1;
                cbxDirection.SelectedIndex = Convert.ToInt16(so.direction_id) - 1;
                cbxSolarHour.SelectedIndex = Convert.ToInt16(so.solar_hour) - 6;
                txtLatitue.Text = so.latitute.ToString();
                txtSolarRadiation.Text = so.radiation_amount.ToString();
            }
        }
        public void newData()
        {
            cbxDescription.Text = "";
            cbxMonth.SelectedItem = 0;
            cbxSolarHour.SelectedIndex = 0;
            cbxDirection.SelectedIndex = 0;
            txtLatitue.Text = "";
            txtSolarRadiation.Text = "";
        }
        public void saveData()
        {
            tb_solar_radiation_through_glass so = new tb_solar_radiation_through_glass();
            so.description = cbxDescription.Text;
            so.direction_id = cbxDirection.SelectedIndex + 1;
            so.month = cbxMonth.SelectedIndex + 1;
            so.solar_hour = cbxSolarHour.SelectedIndex + 6;
            so.latitute = txtLatitue.Text;
            so.radiation_amount = Convert.ToInt16(txtSolarRadiation.Text);

            LibraryManagerInCom.addSolarByRadiation(so);
            reload();
        }
        public void delData()
        {
            tb_solar_radiation_through_glass so = (tb_solar_radiation_through_glass)cbxDescription.SelectedItem;
            LibraryManagerInCom.delSolarByRadiation(so);
            reload();
        }
        public void copyData()
        {
            tb_solar_radiation_through_glass so = (tb_solar_radiation_through_glass)cbxDescription.SelectedItem;
            tb_solar_radiation_through_glass so2 = LibraryManagerInCom.copySolrByRadiation(so);

            cbxDescription.Text = so2.description;
            cbxMonth.SelectedIndex = Convert.ToInt16(so2.month) - 1;
            cbxDirection.SelectedIndex = Convert.ToInt16(so2.direction_id) - 1;
            cbxSolarHour.SelectedIndex = Convert.ToInt16(so2.solar_hour) - 6;
            txtLatitue.Text = so2.latitute.ToString();
            txtSolarRadiation.Text = so2.radiation_amount.ToString();

        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
        
    }
}
